package com.BlogPark.service.ShowArticleService.Impl;

import com.BlogPark.common.utis.IdUtils;
import com.BlogPark.mapper.*;
import com.BlogPark.pojo.Article;
import com.BlogPark.pojo.Comment;
import com.BlogPark.pojo.DetailsUser;
import com.BlogPark.pojo.User;
import com.BlogPark.service.ShowArticleService.ShowArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Auther: MFM
 * @Date: 2022/8/10 - 08 - 10 - 21:50
 * @version: 1.0
 */
@Service("showArticle")
public class ShowArticleServiceImpl implements ShowArticleService {
    //文章对象
    @Autowired
    private Article article;
    //用户对象
    @Autowired
    private DetailsUser detailsUser;
    //获取文章的mapper
    @Autowired
    private ArticleMapper articleMapper;
    //获取用户的mapper
    @Autowired
    private DetailsUserMapper detailsUserMapper;
    //获取点赞的mapper
    @Autowired
    private LikesMapper likesMapper;
    //获取评论的mapper
    @Autowired
    private CommentsMapper commentsMapper;
    //获取收藏的mapper
    @Autowired
    private CollectionsMapper collectionsMapper;

    public Article FindArticle(String articleId) {
        //article是空对象 需要找到给文章
        Article article = articleMapper.selectById(articleId);

        if (article == null) {
            return null;
        }

        article.setAutherName(detailsUserMapper.selectById(article.getAutherId()).getName());

        return article;
    }

    public DetailsUser FindUser(int userId) {
        //detailsUser是空对象 需要找到给作者
        return detailsUserMapper.selectById(userId);
    }

    @Override
    public String ShowTitle(String articleId) throws Exception {
        article = FindArticle(articleId);
        //显示文章时 给文章增加1浏览量
        if (article == null) {
            return "";
        }
        articleMapper.addViews(articleId, article.getViews() + 1);
        return article.getTitle();
    }

    @Override
    public String ShowLabels(String articleId) throws Exception {
        article = FindArticle(articleId);
        if (article == null) {
            return null;
        }
        return article.getLabels();
    }

    @Override
    public int ShowViews(String articleId) throws Exception {
        article = FindArticle(articleId);
        if (article == null) {
            return -1;
        }
        return article.getViews();
    }

    @Override
    public int ShowCollections(String articleId) throws Exception {
        article = FindArticle(articleId);
        if (article == null) {
            return -1;
        }
        return article.getCollection();
    }

    @Override
    public Date ShowDate(String articleId) throws Exception {
        article = FindArticle(articleId);
        if (article == null) {
            return null;
        }
        return article.getDate();
    }

    @Override
    public String ShowContent(String articleId) throws Exception {
        article = FindArticle(articleId);
        if (article == null) {
            return null;
        }
        return article.getContent();
    }

    @Override
    public String ShowNickname(Integer userId) throws Exception {
        detailsUser = FindUser(userId);
        if (detailsUser == null) {
            return null;
        }
        return detailsUser.getName();
    }

    @Override
    public int ShowUserFans(Integer userId) throws Exception {
        detailsUser = FindUser(userId);
        if (detailsUser == null) {
            return -1;
        }
        return detailsUser.getFans();
    }

    @Override
    public int ShowUserViews(Integer userId) throws Exception {
        detailsUser = FindUser(userId);
        if (detailsUser == null) {
            return -1;
        }
        return detailsUser.getViews();
    }

    @Override
    public int ShowUserLikes(Integer userId) throws Exception {
        detailsUser = FindUser(userId);
        if (detailsUser == null) {
            return -1;
        }
        return detailsUser.getLikes();
    }

    @Override
    public int ShowUserComments(Integer userId) throws Exception {
        detailsUser = FindUser(userId);
        if (detailsUser == null) {
            return -1;
        }
        return detailsUser.getComments();
    }

    @Override
    public int ShowUserCollections(Integer userId) throws Exception {
        detailsUser = FindUser(userId);
        if (detailsUser == null) {
            return -1;
        }
        return detailsUser.getCollections();
    }

    public boolean isLikes(User user, String headTo) throws Exception {
        if (user == null) {
            throw new Exception("用户未登录");
        }

        Integer num = likesMapper.getUserLikes(user.getId(), headTo);

        //如果点过赞就不会返回null 如果没点过赞会返回null
        if (num != null) {
            //已经点过赞了
            return true;
        } else {
            //没点过赞呢
            return false;
        }
    }

    @Override
    public void Likes(User user, String articleId) throws Exception {
        if (isLikes(user, articleId)) {
            likesMapper.deleteLike(user.getId(), articleId);
        } else {
            likesMapper.addLike(user.getId(), articleId);
        }
    }

    @Override
    public boolean AddComments(User user, String articleId, String comment) throws Exception {
        return commentsMapper.AddComments(user.getId(), articleId, comment, IdUtils.generateCommentId(user.getId()));
    }

    @Override
    public boolean DeleteComments(User user, String commentId) throws Exception {
        Integer userId = commentsMapper.getUserId(commentId);
        if (userId == null) {
            //没有该评论
            throw new Exception("comment is missing");
        }
        detailsUser = FindUser(user.getId());
        boolean flag = false;
        //只有评论的人或作者才能删除
        if (article.getAutherId() == user.getId() || commentsMapper.getUserId(commentId) == user.getId()) {
            flag = commentsMapper.DeleteComments(user.getId(), commentId);
        }
        return flag;
    }

    @Override
    public boolean AddCollection(User user, String articleId) throws Exception {
        //确保用户没有收藏过
        Integer i = collectionsMapper.getCollection(user.getId(), articleId);

        if (i == null) {
            //没收藏过
            if (collectionsMapper.addCollection(user.getId(), articleId) == 1) {
                //收藏成功
                return true;
            } else {
                //收藏失败
                return false;
            }
        } else {
            //收藏过
            return false;
        }

    }

    @Override
    public boolean DeleteCollection(User user, String articleId) throws Exception {
        return collectionsMapper.deleteCollection(user.getId(), articleId);
    }

    @Override
    public Map<String, List<Comment>> ShowComments(String headTo) throws Exception {
        Map<String, List<Comment>> map = new HashMap<>();
        ArrayList<Comment> list = new ArrayList<>();

        List<Comment> mainList = getList(headTo);

        //主评论
        map.put("mainComment", mainList);

        //子评论
        map.put("childComment", bianli(mainList, headTo, list));

        return map;
    }

    /**
     * 递归查找所有子评论
     *
     * @param list         需要遍历的集合
     * @param headTo       一个指向（本身数值并不重要）
     * @param childComment 子评论集合
     * @return
     */
    public List<Comment> bianli(List<Comment> list, String headTo, List<Comment> childComment) {
        for (Comment comment : list) {
            headTo = comment.getCommentsId();
            List<Comment> getbianli = getList(headTo);
            if (getbianli.size() == 0) {
                //该子评论没有评论了
                continue;
            }
            bianli(getbianli, headTo, childComment);
            childComment.addAll(getbianli);
        }
        return childComment;
    }

    //通过headTo获取评论列表
    public List<Comment> getList(String headTo) {
        List<Comment> allComments = commentsMapper.getAllComments(headTo);
        for (Comment comment : allComments) {
            headTo = comment.getCommentsId();
            comment.setUserName(detailsUserMapper.getName(comment.getUserId()));
            comment.setLikes(likesMapper.getLikes(headTo));
        }
        return allComments;
    }

    @Override
    public List<Article> ShowUserArticle(int userId) throws Exception {
        return articleMapper.selectByUserId(userId);
    }

    @Override
    public boolean isCollection(User user, String articleId) throws Exception {
        Integer num = collectionsMapper.getCollection(user.getId(), articleId);

        //如果点过赞就不会返回null 如果没点过赞会返回null
        if (num != null) {
            //已经收藏了
            return true;
        } else {
            //没收藏呢
            return false;
        }
    }
}
